package com.hongxun.project.assets.pBoxes.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.hongxun.common.utils.text.Convert;
import com.hongxun.project.assets.bByq.domain.BByq;
import com.hongxun.project.assets.bByq.service.IBByqService;
import com.hongxun.project.assets.iLine.domain.ILine;
import com.hongxun.project.assets.iLine.service.IILineService;
import com.hongxun.project.assets.pPoint.domain.PPoint;
import com.hongxun.project.assets.pPoint.service.IPPointService;
//import com.sun.org.apache.bcel.internal.generic.NEW;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.hongxun.framework.aspectj.lang.annotation.Log;
import com.hongxun.framework.aspectj.lang.enums.BusinessType;
import com.hongxun.project.assets.pBoxes.domain.PBoxes;
import com.hongxun.project.assets.pBoxes.service.IPBoxesService;
import com.hongxun.framework.web.controller.BaseController;
import com.hongxun.framework.web.page.TableDataInfo;
import com.hongxun.framework.web.domain.AjaxResult;
import com.hongxun.common.utils.poi.ExcelUtil;

/**
 * 箱采集 信息操作处理
 *
 * @author hongxun
 * @date 2019-03-19
 */
@Controller
@RequestMapping("/assets/pBoxes")
public class PBoxesController extends BaseController {
    private String prefix = "assets/pBoxes";

    @Autowired
    private IPBoxesService pBoxesService;
    @Autowired
    private IPPointService pPointService;
    @Autowired
    private IILineService iLineService;
    @Autowired
    private IBByqService bByqService;


    @RequiresPermissions("assets:pBoxes:view")
    @GetMapping()
    public String pBoxes() {
        return prefix + "/pBoxes";
    }

	/*@GetMapping("/task_pBoxes/{taskId}")
    public String task_pBoxes(@PathVariable("taskId") String taskId , ModelMap modelMap)
	{
		modelMap.put("taskId",taskId);
	    return prefix + "/task-pBoxes";
	}*/

    /**
     * 查询箱采集列表
     */
    @RequiresPermissions("assets:pBoxes:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PBoxes pBoxes) {
        startPage();
        pBoxes.setDatastate("删除");
        List<PBoxes> list = pBoxesService.selectPBoxesList(pBoxes);
        return getDataTable(list);
    }


    /**
     * 导出箱采集列表
     */
    @RequiresPermissions("assets:pBoxes:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PBoxes pBoxes) {
        List<PBoxes> list = pBoxesService.selectPBoxesList(pBoxes);
        ExcelUtil<PBoxes> util = new ExcelUtil<PBoxes>(PBoxes.class);
        return util.exportExcel(list, "pBoxes");
    }

    /**
     * 新增箱采集
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存箱采集
     */
    @RequiresPermissions("assets:pBoxes:add")
    @Log(title = "箱采集", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PBoxes pBoxes) {
        return toAjax(pBoxesService.insertPBoxes(pBoxes));
    }

    /**
     * 修改箱采集
     */
    @GetMapping("/edit/{id}/{taskId}")
    public String edit(@PathVariable("id") String id, @PathVariable("taskId") String taskId, ModelMap mmap) {
        PBoxes pBoxes = pBoxesService.selectPBoxesByIdForState(id, taskId);
        PPoint pPoint = pPointService.selectPPointByIdForState(pBoxes.getPId(),taskId);
        ILine iLine = iLineService.selectILineById(pBoxes.getLineId());
        pBoxes.setPId(pPoint.getCode());
//		pBoxes.setLineId(iLine.getName());
        mmap.put("pBoxes", pBoxes);
        return prefix + "/edit";
    }

    /**
     * 修改保存箱采集
     */
    @RequiresPermissions("assets:pBoxes:edit")
    @Log(title = "箱采集", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PBoxes pBoxes) {
        PBoxes pBoxes1 = pBoxesService.selectPBoxesById(pBoxes.getId(), pBoxes.getTaskId());
        PPoint pPoint = pPointService.selectPPointByIdForState(pBoxes1.getPId() , pBoxes.getTaskId());
        pBoxes.setPId(pBoxes1.getPId());
        pBoxes.setByqId(pPoint.getByqId());
		pBoxes.setLineId(pPoint.getLineId());
		pBoxes.setUpdateUser(Integer.parseInt(getUserId().toString()));
		pBoxes.setUpdateDate(new Date());
        pBoxes.setDatastate("修改");
        return toAjax(pBoxesService.updatePBoxes(pBoxes));
    }

    /**
     * 删除箱采集
     */
    @RequiresPermissions("assets:pBoxes:remove")
    @Log(title = "箱采集", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(pBoxesService.deletePBoxesByIds(ids));
    }


    /**
     * 删除箱采集
     */
//    @RequiresPermissions("assets:pBoxes:remove")
    @Log(title = "箱采集", businessType = BusinessType.DELETE)
    @PostMapping("/remove2Id")
    @ResponseBody
    public AjaxResult remove2Id(String ids , String taskIds) {

        String[] str = Convert.toStrArray(ids);
        int i = 0;
        for (String s : str) {
            PBoxes pBoxes = new PBoxes();
            pBoxes.setId(s);
            pBoxes.setTaskId(taskIds);
            pBoxes.setDatastate("删除");
            i = pBoxesService.updatePBoxes(pBoxes);
        }
        return toAjax(i);
//        return toAjax(pBoxesService.deletePBoxesByIdsAndTaskIds(ids , taskIds));
    }

}
